package com.orcsystem.product.app;

import com.orcsystem.common.utils.PageUtils;
import com.orcsystem.common.utils.R;
import com.orcsystem.product.app.vo.AttrRespVo;
import com.orcsystem.product.app.vo.AttrVo;
import com.orcsystem.product.service.AttrAttrgroupRelationService;
import com.orcsystem.product.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 商品属性
 *
 * @author aihs
 * @email aihs_work@163.com
 * @date 2020-04-29 14:16:13
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catid}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                           @PathVariable("type") String type,
                           @PathVariable("catid") Long catid){
        PageUtils page = attrService.queryBaseAttrPage(params,type,catid);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
        System.out.println("attr = " + attr);
        attrService.saveAtter(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
